package com.winit.openapi.rma.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.rma.RMAOrderService;
import com.winit.oms.spi.rma.RMAOrderTrackingService;
import com.winit.oms.spi.rma.command.QueryRMATrackingCommand;
import com.winit.oms.spi.rma.command.RMAOrderCommand;
import com.winit.oms.spi.rma.vo.RMADischargeCargoVo;
import com.winit.oms.spi.rma.vo.RMAOrderTrackingVo;
import com.winit.oms.spi.rma.vo.RMAOrderVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.tp.spi.common.TPContants;

@Controller
@RequestMapping(value = "/rma/tracking")
public class RmaOrderTrackingController extends BaseController {

    @Resource
    private RMAOrderTrackingService rmaOrderTrackingService;

    @Resource
    private RMAOrderService         returnOrderService;

    @RequestMapping(value = "/queryOrderTracking")
    public String queryOrderDetails(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        String orderNo = "";
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "returnGoodsOrderNo");
            orderNo = json.getString("returnGoodsOrderNo");
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 查询订单轨迹
        QueryRMATrackingCommand command = new QueryRMATrackingCommand();
        command.setOrderNo(orderNo);
        List<RMAOrderTrackingVo> tList = rmaOrderTrackingService.queryTrackingList(command);
        Map<String, Object> map = new HashMap<String, Object>();
        if (tList != null && tList.size() > 0) {
            List<Map<String, Object>> dataList = buildRmaOrderTrackingList(tList, null);
            map.put("trackingList", dataList);
        }
        responseMsg.setData(map);
        return SUCCESS;
    }

    @RequestMapping(value = "/queryReturnTracking")
    public String queryReturnTracking(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	 Object obj = requestMsg.getData();
         String trackingNo = "";
         String warehouseCode = "";
         if (obj instanceof JSONObject) {
             JSONObject json = (JSONObject) obj;
             // 快递单号
             ValidateUtil.validNotNull(json, "trackingNo");
             trackingNo = json.getString("trackingNo");
             // 仓库编码
             ValidateUtil.validNotNull(json, "warehouseCode");
             warehouseCode = json.getString("warehouseCode");
         } else {
             throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
         }
         QueryRMATrackingCommand command = new QueryRMATrackingCommand();
         command.setTrackingNo(trackingNo);
         command.setWarehouseCode(warehouseCode);
         // 查询卸货信息
         List<RMADischargeCargoVo> tList = rmaOrderTrackingService.getByTrackingNoAndWCode(command);
         Map<String, Object> map = new HashMap<String, Object>();
         if (tList == null || tList.size() == 0) {
             responseMsg.setData(null);
             return SUCCESS;
         }
         map.put("dischargeCargo", buildRmaOrderList(tList));
         RMAOrderCommand command1 = new RMAOrderCommand();
         RMAOrderVo vo = new RMAOrderVo();
         vo.setWarehouseCode(warehouseCode);
         vo.setExpressNo(trackingNo);
         command1.setReturnOrderVo(vo);
         List<RMAOrderVo> orderList = returnOrderService.querytReturnOrder(command1);
         if (null != orderList && orderList.size() > 0) {
             RMAOrderVo orderVo = orderList.get(0);
             command = new QueryRMATrackingCommand();
             command.setOrderNo(orderVo.getRmaOrderNo());
             List<RMAOrderTrackingVo> tackingList = rmaOrderTrackingService.getByTrackingByOrderNo(command);
             List<Map<String, Object>> dataList = buildRmaOrderTrackingList(tackingList,
                 orderVo.getDestroyOutboundOrder());
             map.put("trackingList", dataList);
         }
         responseMsg.setData(map);
         return SUCCESS;
    }

    /**
     * 构建订单轨迹
     * 
     * @return
     */
    private List<Map<String, Object>> buildRmaOrderTrackingList(List<RMAOrderTrackingVo> tList,
                                                                String destroyOutboundOrder) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (RMAOrderTrackingVo vo : tList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("date", vo.getTrackingDate());
            map.put("trackingDesc", vo.getTrackingDesc());
            map.put("location", vo.getTrackingLocation());
            map.put("trackingCode", vo.getTrackingCode());
            if (TPContants.TrckingCodeEnum.RETURN_DESTROY.getCode().equals(vo.getTrackingCode())) {
                map.put("imageList", vo.getImageList());
            }
            if (TPContants.TrckingCodeEnum.RMA_SHELVES.getCode().equals(vo.getTrackingCode())) {
                map.put("orderNo", vo.getRmaOrderNo());
            }
            if (TPContants.TrckingCodeEnum.RETURN_DESTROY_OUTBOUND.getCode().equals(vo.getTrackingCode())) {
                map.put("destroyOutboundOrder", destroyOutboundOrder);
            }
            mapList.add(map);
        }
        return mapList;

    }

    private Map<String, Object> buildRmaOrderList(List<RMADischargeCargoVo> tList) {
        Map<String, Object> map = new HashMap<String, Object>();
        RMADischargeCargoVo vo = tList.get(0);
        map.put("date", vo.getDischargeDate());
        map.put("trackingDesc", vo.getDischargeDesc());
        map.put("trackingCode", vo.getDischargeCode());
        return map;

    }
}
